<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket Chat</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #f0f0f0;
        }

        .container {
            width: 100%;
            max-width: 800px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 20px;
            box-sizing: border-box;
        }

        .chat-history {
            height: 300px;
            overflow-y: auto;
            margin-bottom: 20px;
        }

        .message {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 10px;
        }

        .message.sent {
            background-color: #dcf8c6;
            text-align: right;
        }

        .message.received {
            background-color: #f1f0f0;
            text-align: left;
        }

        .input-container {
            display: flex;
            margin-bottom: 10px;
        }

        .input-container input {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-right: 10px;
        }

        .input-container button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .connection-status {
            text-align: center;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div>
            <h1 style="text-align: center;">WebSocket聊天室测试环境</h1>
        </div>
        <div class="input-container">
            <input type="text" id="token-input" placeholder="Enter Token">
            <button id="connect-btn">连接</button>
            <button id="disconnect-btn" style="margin-left: 10px;">断开</button>
        </div>
        <div class="connection-status">
            <label id="connection-status">状态：未连接</label>
            <label>用户Id：<span id="user-id"></span></label>
            <label>用户昵称：<span id="user-nickname"></span></label>
        </div>
        <!-- <div id="user-info">
            <label>用户Id: <span id="user-id"></span></label>
            <label>用户昵称: <span id="user-nickname"></span></label>
        </div> -->
        <div class="chat-history" id="chat-history"></div>
        <div class="input-container">
            <input type="text" id="receiver-id-input" style="width: 20px;"  placeholder="Receiver ID">
            <input type="text" id="message-input" placeholder="Message">
            <button id="send-btn">Send</button>
            <button id="clear-chat-btn" style="margin-left: 10px;">Clear Chat</button>
        </div>

    </div>

    <script>
        let webSocket;
        let userId = null;
        let userNickname = null;

        document.getElementById('connect-btn').addEventListener('click', connectWebSocket);
        document.getElementById('disconnect-btn').addEventListener('click', disconnectWebSocket);
        document.getElementById('send-btn').addEventListener('click', sendMessage);
        document.getElementById('clear-chat-btn').addEventListener('click', clearChatHistory);

        function connectWebSocket() {
            const token = document.getElementById('token-input').value;
            const wsUrl = `ws://117.34.99.190:8083/api/chat-socket?token=${token}`;

            webSocket = new WebSocket(wsUrl);

            webSocket.onopen = () => {
                document.getElementById('connection-status').textContent = '状态：已连接';
            };

            webSocket.onmessage = (event) => {
                const dataJson = JSON.parse(event.data);
                const data = dataJson.messageData;
                if (data.userId && data.userNickname) {
                    userId = data.userId;
                    userNickname = data.userNickname;
                    document.getElementById('user-id').textContent = userId;
                    document.getElementById('user-nickname').textContent = userNickname;
                } else {
                    const message = `${data.senderNickname}(${data.sendTime}): ${data.content}`;
                    const messageElement = document.createElement('div');
                    messageElement.classList.add('message', data.senderId === userId ? 'sent' : 'received');
                    messageElement.textContent = message;
                    document.getElementById('chat-history').appendChild(messageElement);
                }
            };

            webSocket.onclose = () => {
                document.getElementById('connection-status').textContent = '状态：未连接';
            };
        }

        function disconnectWebSocket() {
            if (webSocket && webSocket.readyState === WebSocket.OPEN) {
                webSocket.close();
            }
        }

        function sendMessage() {
            const receiverId = document.getElementById('receiver-id-input').value;
            const message = document.getElementById('message-input').value;

            const payload = {
                chatType: 1,
                receiverId: parseInt(receiverId),
                contentType: 1,
                content: message
            };

            if (webSocket && webSocket.readyState === WebSocket.OPEN) {
                webSocket.send(JSON.stringify(payload));
            }
        }

        function clearChatHistory() {
            document.getElementById('chat-history').innerHTML = '';
        }
    </script>
</body>
</html>